﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Domain.zmc;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Productionwork;
using MyMES.Read.API.Dtos;
using MySqlX.XDevAPI.Common;

namespace MyMES.Read.API.Application.CommendHandlers.Productionwork
{
    public class GetProductionordersCommendHandlers : IRequestHandler<GetProductionordersCommends, APIPaging<List<ProducStoresWorkOrdeDto>>>
    {


        private readonly IBaseRepository<woekderProess> workderRepository;
        private readonly IBaseRepository<Productionorders> productionorderRepository;
      
        private readonly IBaseRepository<Materialmanagement> materiasRepository;
        private readonly IBaseRepository<WorkOrder> workordersRepository;

  
    
        private readonly IMapper mapper;

        public GetProductionordersCommendHandlers(IBaseRepository<woekderProess> workderRepository, IBaseRepository<Productionorders> productionorderRepository, IBaseRepository<Materialmanagement> materiasRepository, IBaseRepository<WorkOrder> workordersRepository, IMapper mapper)
        {
            this.workderRepository = workderRepository;
            this.productionorderRepository = productionorderRepository;
            this.materiasRepository = materiasRepository;
            this.workordersRepository = workordersRepository;
            this.mapper = mapper;
        }




        /// <summary>
        /// 获取生产订单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<ProducStoresWorkOrdeDto>>> Handle(GetProductionordersCommends request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<ProducStoresWorkOrdeDto>> result = new APIPaging<List<ProducStoresWorkOrdeDto>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "查询成功";
                var sql = "SELECT a.*,b.Materialnumber,b.Materialname,b.Specification,b.Unit,b.InboundNumber,c.WorkOrderCoding,c.WorkOrderName,d.ProductionordersNo,d.ProductionordersName ,d.Head,d.Department ,d.ProductionordersStatus,d.PlanStartTime,d.PlanEndTime,d.yield FROM woekderproess a LEFT JOIN materialmanagement b on a.MId=b.MId  LEFT JOIN workorder c on a.WorkOrderId=c.WorkOrderId  LEFT JOIN productionorders d on a.ProductionordersId=d.ProductionordersId    where a.IsDelete=false ORDER BY a.woekderProessId DESC";

                var workder = await workderRepository.SelectAsync(sql);
                var productions = await productionorderRepository.SelectAsync(sql);
                var materias = await materiasRepository.SelectAsync(sql);
                var workorders = await workordersRepository.SelectAsync(sql);


                var workders = mapper.Map<List<ProducStoresWorkOrdeDto>>(workder);
                var produc = mapper.Map<List<ProducStoresWorkOrdeDto>>(productions);
                var rarriva = mapper.Map<List<ProducStoresWorkOrdeDto>>(materias);
                var workorderss = mapper.Map<List<ProducStoresWorkOrdeDto>>(workorders);



                //循环赋值
                foreach (var item in workders)
                {
                    item.ProductionordersNo = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.ProductionordersNo;
                    item.ProductionordersName = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.ProductionordersName;
                    item.Head = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.Head;
                    item.Department = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault()?.Department;
                    item.ProductionordersStatus = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().ProductionordersStatus;
                    item.yield = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().yield;
                    item.PlanStartTime = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().PlanStartTime;
                    item.PlanEndTime = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().PlanEndTime;

                    item.Materialnumber = rarriva.Where(x => x.MId == item.MId).FirstOrDefault()?.Materialnumber;
                    item.Materialname = rarriva.Where(x => x.MId == item.MId).FirstOrDefault()?.Materialname;
                    item.Specification = rarriva.Where(x => x.MId == item.MId).FirstOrDefault()?.Specification;
                    item.Unit = rarriva.Where(x => x.MId == item.MId).FirstOrDefault().Unit;
          item.InboundNumber = rarriva.Where(x => x.MId == item.MId).FirstOrDefault().InboundNumber;


                    item.WorkOrderCoding = workorderss.Where(x => x.WorkOrderId == item.WorkOrderId).FirstOrDefault()?.WorkOrderCoding;
                    item.WorkOrderName = workorderss.Where(x => x.WorkOrderId == item.WorkOrderId).FirstOrDefault()?.WorkOrderName;
                }


                if (!string.IsNullOrEmpty(request.ProductionordersName))
                {
                    workders = workders.Where(x => x.ProductionordersName.Contains(request.ProductionordersName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ProductionordersNo))
                {
                    workders = workders.Where(x => x.ProductionordersNo.Contains(request.ProductionordersNo)).ToList();
                }
       
                result.TotalCount = workders.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                workders = workders.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                result.data = workders;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
